package light.head.controller.el;

import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import light.head.constant.Msg;
import light.head.constant.Parm;
import light.head.constant.Url;
import light.head.dao.el.AnswerRecordDAO;
import light.head.dao.el.ClassesDAO;
import light.head.dao.el.CourseDAO;
import light.head.dao.el.ExamRecordDAO;
import light.head.dao.el.TopicAnswerDAO;
import light.head.dao.el.TopicDAO;
import light.head.dao.el.TopicRecordDAO;
import light.head.dao.el.UserClassesRelationDAO;
import light.head.dao.sys.OrganDAO;
import light.head.enm.RESULT;
import light.head.framework.base.SupporterController;
import light.head.framework.entity.PageBean;
import light.head.model.el.AnswerRecord;
import light.head.model.el.ExamRecord;
import light.head.model.el.TopicRecord;
import light.head.model.el.UserClassesRelation;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

/**
 * 阅卷管理
 */
@Controller
@RequestMapping(Url.C_MARKTOPIC)
public class MarkTopicController extends SupporterController{
	private ClassesDAO cDAO;
	private UserClassesRelationDAO ucrDAO;
	private ExamRecordDAO erDAO;
	private TopicRecordDAO trDAO;
	private AnswerRecordDAO arDAO;
	private TopicDAO tDAO;
	private TopicAnswerDAO tkDAO;
	private CourseDAO courseDAO;
	private OrganDAO organDAO;
	
	@RequestMapping(Url.INIT)
	public ModelAndView init(HttpServletRequest request, HttpServletResponse response){
		return new ModelAndView(Url.MARKTOPIC_INIT);
	}
	
	@RequestMapping(Url.QUERY)
	public ModelAndView query(HttpServletRequest request, HttpServletResponse response){
		PageBean pb = createPageBean(request);
		Map<String, String> map = createQueryMapByParmName(request, "clc","cc",Parm.OC,Parm.CU,Parm.CT, Parm.DELETED);
		erDAO.findByMap(cu_organCode(request), map, pb);
		request.setAttribute(Parm.PAGE_BEAN, pb);
		return new ModelAndView(Url.MARKTOPIC_LIST);
	}
	/**
	 * 查询班级情况
	 *//*
	@RequestMapping(Url.MARKTOPICQUERYClASS)
	public ModelAndView queryClass(HttpServletRequest request, HttpServletResponse response){
		String name = request.getParameter(Parm.NAME);
		List<Classes> list = cDAO.findMyOcAndNamelasses(cu_organCode(request),name);
		request.setAttribute(Parm.LIST, list);
		return new ModelAndView(Url.MARKTOPICQUERY_ClASS);
	}*/
	/**
	 * 查询学生情况
	 */
	@RequestMapping(Url.MARKTOPICQUERYSTUDENT)
	public ModelAndView queryStudent(HttpServletRequest request, HttpServletResponse response){
		String name = request.getParameter(Parm.NAME);
		String code = request.getParameter(Parm.CODE);
		List<UserClassesRelation> list = ucrDAO.findByClcAndName(Integer.parseInt(code),name,cu_organCode(request));
		request.setAttribute(Parm.USER_LIST, list);
		return new ModelAndView(Url.MARKTOPICQUERY_STUDENT);
	}
	/**
	 * 查询课程 及 学习情况 和 考试记录
	 */
	/*@RequestMapping(Url.MARKTOPICQUERYCOURSE)
	public ModelAndView queryCourse(HttpServletRequest request, HttpServletResponse response){
		String clc = request.getParameter(Parm.CLC);
		String uc = request.getParameter(Parm.UC);
		if(clc != null && uc != null){
			Integer clCode = Integer.parseInt(clc);
			Integer uCOde = Integer.parseInt(uc);
			//查询课程
			List<ClassesCourseRelation> cList = ccrDAO.findByCcrCode(Integer.parseInt(clc));
			if(cList != null && cList.size() > 0){
				for(ClassesCourseRelation ccr : cList){
					//最好成绩
					ccr.setBestEr(erDAO.findBestScoreInfo(clCode,uCOde,ccr.getCc()));
					//考试记录
					ccr.setList(erDAO.findByClcAndUcAndCc(clCode,uCOde,ccr.getCc()));
					//学习情况
					ccr.setLearnList(lDAO.findByClcAndUcAndCc(clCode, uCOde,ccr.getCc()));
				}
			}
			request.setAttribute(Parm.COURSELIST,cList);
		}
	
		return new ModelAndView(Url.MARKTOPICQUERY_COURSE);
	}*/
	
	/**
	 * 阅卷
	 */
	@RequestMapping(Url.MARKTOPICMARKINFO)
	public ModelAndView markTopicInfo(HttpServletRequest request, HttpServletResponse response){
		String code = request.getParameter(Parm.CODE);
		if(isNotNull(code)){
			Integer erc = Integer.parseInt(code);
			ExamRecord er = erDAO.get(erc);
			List<TopicRecord> trList = trDAO.findByErc(erc);
			if(trList != null && trList.size() > 0){
				for(TopicRecord tr : trList){
					List<AnswerRecord> list = arDAO.findByTrcAndErcAndTc(tr.getCode(), erc, tr.getTc());
					tr.setArList(list);
					tr.setTopic(tDAO.get(tr.getTc()));
					if(tr.getTopic() != null){
						tr.getTopic().setItemList(tkDAO.findKeyByCode(tr.getTopic().getCode()));
					}
				}
			}
			request.setAttribute(Parm.INFO, er);
			request.setAttribute(Parm.TRLIST, trList);
		}
		return new ModelAndView(Url.MARKTOPIC_MARK);
	}
	
	/**
	 * 保存阅卷
	 */
	@RequestMapping(Url.SAVE)
	@Transactional(rollbackFor = Exception.class)
	public void save(ExamRecord er ,HttpServletRequest request, HttpServletResponse response){
		RESULT result = RESULT.ERROR;
		if(er != null){
			er.setMarkCt(new Date());
			er.setMarkCu(cu_code(request));
			er.setIsMark(false);
			result = erDAO.update(er);
		}
		if(result == RESULT.SUCCESS){
			/*result = RESULT.ERROR;*/
			if(er.getTrList() != null && er.getTrList().size() > 0){
				for(TopicRecord tr : er.getTrList()){
					trDAO.update(tr);
				}
				/*result = RESULT.SUCCESS;*/
			}
		}
		if(result == RESULT.SUCCESS){
			print(response,Msg.SUCCESS);
		}else{
			print(response,Msg.ERROR);
		}
	}
	
	
	//选择课程
	@RequestMapping(Url.AID_INIT)
	public ModelAndView aidInit(HttpServletRequest request, HttpServletResponse response){
		return new ModelAndView(Url.MARKTOPIC_COURSE_INIT);
	}
	@RequestMapping(Url.AID_LIST)
	public ModelAndView aidList(HttpServletRequest request, HttpServletResponse response){
		PageBean pb = createPageBean(request);
		Map<String, String> map = createQueryMapByParmName(request, Parm.NAME);
		Integer oc = organDAO.getRoot(cu_organCode(request)).getCode();
		courseDAO.findByMap(oc, map, pb);
		request.setAttribute(Parm.PAGE_BEAN, pb);
		return new ModelAndView(Url.MARKTOPIC_COURSE_LIST);
	}
	
	//选择班级
	@RequestMapping(Url.AID_INIT_1)
	public ModelAndView aidInit1(HttpServletRequest request, HttpServletResponse response){
		return new ModelAndView(Url.MARKTOPIC_CLASSES_INIT);
	}
	
	@RequestMapping(Url.AID_LIST_1)
	public ModelAndView aidList1(HttpServletRequest request, HttpServletResponse response){
		PageBean pb = createPageBean(request);
		Map<String, String> map = createQueryMapByParmName(request, Parm.ON, Parm.NAME);
		Integer oc = organDAO.getRoot(cu_organCode(request)).getCode();
		cDAO.findByOc(oc, map, pb);
		request.setAttribute(Parm.PAGE_BEAN, pb);
		return new ModelAndView(Url.MARKTOPIC_CLASSES_LIST);
	}
	// 选择用户
	@RequestMapping(Url.AID_INIT_2)
	public ModelAndView aidInit2(HttpServletRequest request, HttpServletResponse response){
		return new ModelAndView(Url.MARKTOPIC_USER_INIT);
	}
	
	@RequestMapping(Url.AID_LIST_2)
	public ModelAndView aidList2(HttpServletRequest request, HttpServletResponse response){
		PageBean pb = createPageBean(request);
		Map<String, String> map = createQueryMapByParmName(request, Parm.ON, Parm.REAL_NAME);
		Integer oc = organDAO.getRoot(cu_organCode(request)).getCode();
		ucrDAO.findByUc(oc, map, pb);
		request.setAttribute(Parm.PAGE_BEAN, pb);
		return new ModelAndView(Url.MARKTOPIC_USER_LIST);
	}	
		
	@Autowired
	public void setcDAO(ClassesDAO cDAO) {
		this.cDAO = cDAO;
	}
	@Autowired
	public void setUcrDAO(UserClassesRelationDAO ucrDAO) {
		this.ucrDAO = ucrDAO;
	}
	@Autowired
	public void setErDAO(ExamRecordDAO erDAO) {
		this.erDAO = erDAO;
	}
	@Autowired
	public void setTrDAO(TopicRecordDAO trDAO) {
		this.trDAO = trDAO;
	}
	@Autowired
	public void setArDAO(AnswerRecordDAO arDAO) {
		this.arDAO = arDAO;
	}
	@Autowired
	public void settDAO(TopicDAO tDAO) {
		this.tDAO = tDAO;
	}
	@Autowired
	public void setTkDAO(TopicAnswerDAO tkDAO) {
		this.tkDAO = tkDAO;
	}
	@Autowired
	public void setCourseDAO(CourseDAO courseDAO) {
		this.courseDAO = courseDAO;
	}
	@Autowired
	public void setOrganDAO(OrganDAO organDAO) {
		this.organDAO = organDAO;
	}
	
}
